import pinyin from "pinyin";
import $ from "jquery";

const mixins = {
    computed: {
        content: {
            get: function () {
                return this.$store.getters.content
            },
            set: function (val) {
                this.$store.dispatch('audio/setcontents', val)
            }
        },
    },
    methods: {
        // 渲染
        setContent() {
            let p = "";
            this.strData.forEach((row, i) => {
                let ret = "";
                row.forEach((col, index) => {
                    ret += `<div class="${col.className}" data_col="${index}"><${col.tag
                        } class="word">${col.isDuo ? `<b class="textPinyin">${col.isDuo}</b>` : ""
                        }<i class="text ${col.isnum && "isnum"}  ${col.isShd && "isShd"} ${col.isDuo && "isDuo"
                        }" lang="${index + 1}" row="${i}"> ${col.text == " " ? "&nbsp;" : col.text
                        }</i></${col.tag}></div>`;
                });
                p += `<div class="editor_row" data_row="${i}"><div class="word_box">${ret}</div></div>`;
                this.content = p;
            });
        },
        pinyinDo(str) {
            let pinyinArr = [];
            if (/^[\u4e00-\u9fa5]+$/.test(str)) {
                const strArr = pinyin(str, {
                    heteronym: true, // 启用多音字模式
                });
                pinyinArr = strArr[0];
            }
            return pinyinArr;
        },
        // 移动光标
        runRange() {
            this.$nextTick(() => {
                const nodes = $("#editor_content")
                    .children(".editor_row")
                    .eq(this.pos.row)
                    .children(".word_box")
                    .eq(0);
                const sel = window.getSelection();
                const range = document.createRange();
                range.setStart(nodes[0], this.pos.col);
                range.collapse(true);
                sel.removeAllRanges();
                sel.addRange(range);
            });
        },
    }
}
export default mixins